//
//  MZExtensionUIView.swift
//  MZTool
//
//  Created by gby on 2022/3/7.
//

import UIKit

extension MZKit where Base == UIView {
    
    public var left: CGFloat{
        get{
            return base.frame.origin.x
        }
        set{
            var frame = base.frame
            frame.origin.x = newValue
            base.frame = frame
        }
    }
    
    public var right: CGFloat{
        get{
            return base.frame.origin.x + base.frame.size.width
        }
        set{
            var frame = base.frame
            frame.origin.x = newValue - frame.width
            base.frame = frame
        }
    }
    
    public var top: CGFloat{
        get{
            return base.frame.origin.y
        }
        set{
            var frame = base.frame
            frame.origin.y = newValue
            base.frame = frame
        }
    }
    
    public var width: CGFloat{
        get{
            return base.frame.size.width
        }
        set{
            var frame = base.frame
            frame.size.width = newValue
            base.frame = frame
        }
    }
    
    public var height: CGFloat{
        get{
            return base.frame.size.height
        }
        set{
            var frame = base.frame
            frame.size.height = newValue
            base.frame = frame
        }
    }
    
    public var bottom: CGFloat{
        get{
            return base.frame.size.height + base.frame.origin.y
        }
        set{
            var frame = base.frame
            frame.origin.y = newValue - frame.size.height
            base.frame = frame
        }
    }
    
    public var size: CGSize{
        get{
            return base.frame.size
        }
        set{
            var frame = base.frame
            frame.size = newValue
            base.frame = frame
        }
    }
    
    public func viewController() -> UIViewController? {
        for view in sequence(first: base.superview, next: {$0?.superview}){
            if let responder = view?.next{
                if responder.isKind(of: UIViewController.self){
                    return responder as? UIViewController
                }
            }
        }
        return nil
    }
    
    //MARK: 抖动
    public func sharkingAnimate() {
        let viewlayer: CALayer = base.layer
        let position: CGPoint = viewlayer.position
        let x = CGPoint(x: position.x + 10, y: position.y)
        let y = CGPoint(x: position.x - 10, y: position.y)
        
        let animation:CABasicAnimation = CABasicAnimation(keyPath: "position")
        animation.timingFunction = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.default)
        animation.fromValue = NSValue(cgPoint: x)
        animation.toValue = NSValue(cgPoint: y)
        animation.autoreverses = true
        animation.duration = 0.06
        animation.repeatCount = 3.0
        viewlayer.add(animation, forKey: nil)
    }
    
    //MARK: 根据宽高比返回固定宽度下的大小
    public func ratioHieght(_ toWidth:CGFloat ) -> CGSize{
        return UIView.mz.ratioHieght(base.mz.size, toWidth);
    }
    //MARK: 根据宽高比返回固定高度下的大小
    public func ratioWidth(_ toHeight:CGFloat ) -> CGSize{
        return UIView.mz.ratioWidth(base.mz.size, toHeight);
    }
    //MARK: 根据最大size ，返回比例后size
    public func ratioSize(_ toMaxSize:CGSize ) -> CGSize{
        return UIView.mz.ratioSize(base.mz.size, toMaxSize);
    }
    
    //MARK: 快照
    public func viewToImg() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(base.frame.size, false, 3.0)
        defer {
            UIGraphicsEndImageContext()
        }
        guard let ctx = UIGraphicsGetCurrentContext() else {
            return nil
        }
        base.layer.render(in: ctx)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        return img
    }
    
    public func createShadow(cornerRadius: CGFloat, color: UIColor, radius: CGFloat, offset:CGSize, opacity: Float){
        base.layer.cornerRadius = cornerRadius
        base.layer.shadowColor = color.cgColor
        base.layer.shadowRadius = radius
        base.layer.shadowOffset = offset
        base.layer.shadowOpacity = opacity
    }
    
    
    
    
    
    
    
    
    
    
    //MARK: 根据宽高比返回固定宽度下的大小
    public static func ratioHieght(_ size:CGSize ,_ toWidth:CGFloat ) -> CGSize{
        guard size.height != 0 && size.width != 0 else {
            return CGSize(width: toWidth, height: size.height)
        }
        let scale:CGFloat = (toWidth * 1.0)/size.width
        return CGSize(width: toWidth, height: ceil(size.height * scale))
    }
    //MARK: 根据宽高比返回固定高度下的大小
    public static func ratioWidth(_ size:CGSize ,_ toHeight:CGFloat ) -> CGSize{
        guard size.height != 0 && size.width != 0 else {
            return CGSize(width: size.width, height: toHeight)
        }
        let scale:CGFloat = (toHeight * 1.0)/size.height
        return CGSize(width: ceil(size.width * scale), height: toHeight)
    }
    //MARK: 根据最大size ，返回比例后size
    public static func ratioSize(_ size:CGSize ,_ toMaxSize:CGSize ) -> CGSize{
        guard size.height != 0 && size.width != 0 else {
            return size
        }
        let scaleW:CGFloat = (toMaxSize.height * 1.0)/size.height
        let scaleH:CGFloat = (toMaxSize.width * 1.0)/size.width
        let scale = min(scaleW,scaleH)
        return CGSize(width: ceil(size.width * scale), height: ceil(size.height * scale))
    }
    
    //MARK: 根据屏幕 ，返回比例后size
    public static func ratioSize(_ size:CGSize) -> CGSize{
        //已iphone 6 为基准
        let defscale:CGFloat = 375.0 / 667.0
        let scale:CGFloat = UIScreen.main.bounds.width / UIScreen.main.bounds.height
        return CGSize(width: ceil(size.width * (defscale / scale)), height: ceil(size.height * (defscale / scale)))
    }
}
